﻿Public Class ListView
    Inherits Windows.Forms.ListView

    Event Activate(ByVal sender As Object, ByVal e As EventArgs)

    Sub New()
        DoubleBuffered = True
    End Sub

    Private Sub List_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles Me.ColumnClick
        Sorting = If(Sorting = SortOrder.Ascending, SortOrder.Descending, SortOrder.Ascending)
        ListViewItemSorter = New ListViewColumnSorter(e.Column, Sorting)
        Sort()
    End Sub

    Overloads Sub Sort(ByVal SortColumn As Integer)
        BeginUpdate()
        ListViewItemSorter = New ListViewColumnSorter(SortColumn, SortOrder.Ascending)
        Sort()
        EndUpdate()
    End Sub

    Private Sub List_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DoubleClick
        RaiseEvent Activate(Me, Nothing)
    End Sub

    Private Sub List_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.Handled = True
            RaiseEvent Activate(Me, Nothing)
        End If
    End Sub

End Class

Public Class ListViewColumnSorter
    Implements System.Collections.IComparer

    Public SortColumn As Integer = 0
    Public SortOrder As SortOrder = SortOrder.None
    Private Comparer As New CaseInsensitiveComparer

    Sub New(ByVal Column As Integer, ByVal Order As SortOrder)
        SortColumn = Column
        SortOrder = Order
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Return If(SortOrder = SortOrder.Ascending, 1, -1) * Comparer.Compare(x.SubItems(SortColumn).Text, y.SubItems(SortColumn).Text)
    End Function

End Class

Public Class Panel
    Inherits System.Windows.Forms.Panel

    Sub New()
        DoubleBuffered = True
    End Sub

End Class